<template>


  <div class="product-add">
    <!--form表单-->
    <el-form size="small" ref="form" :model="form" label-width="200px">
      <!--添加门店-->
      <div class="common-form">新增小票打印機</div>
      <el-form-item label="打印機名稱" prop="printerName" :rules="[{required: true,message: ' '}]">
        <el-input v-model="form.printerName" class="max-w460"></el-input>
      </el-form-item>
      <el-form-item label="打印機類型" :rules="[{required: true,message: ' '}]">
        <el-select v-model="form.printerType" placeholder="請選擇">
          <el-option v-for="(item,index) in type" :key="index" :label="item" :value="item">
          </el-option>
        </el-select>
        <div class="tips">目前支援 飛鵝打印機、芯燁打印機</div>
      </el-form-item>


      <!-- 飞鹅打印机 -->
      <div v-if="form.printerType == '飛鵝打印機'">
        <el-form-item label="USER"  prop="feieYun.USER" :rules="[{required: true,message: ' '}]">
          <el-input v-model="form.feieYun.USER" class="max-w460"></el-input>
          <div class="tips">飛鵝雲後台註冊用戶名</div>
        </el-form-item>

        <el-form-item label="UKEY"  prop="feieYun.UKEY" :rules="[{required: true,message: ' '}]">
          <el-input v-model="form.feieYun.UKEY" class="max-w460"></el-input>
          <div class="tips">飛鵝雲後台登入生成的UKEY</div>
        </el-form-item>

        <el-form-item label="打印機編號"  prop="feieYun.SN" :rules="[{required: true,message: ' '}]">
          <el-input v-model="form.feieYun.SN" class="max-w460"></el-input>
          <div class="tips">打印機編號為9位數字，查看飛鵝打印機底部貼紙上的編號</div>
        </el-form-item>
      </div>

      <!-- 365云打印 -->
      <div v-if="form.printerType == '365雲打印'">
        <el-form-item label="打印機編號"  prop="printCenter.deviceNo" :rules="[{required: true,message: ' '}]">
          <el-input v-model="form.printCenter.deviceNo" class="max-w460"></el-input>
        </el-form-item>

        <el-form-item label="打印機密鑰" prop="printCenter.key" :rules="[{required: true,message: ' '}]">
          <el-input v-model="form.printCenter.key" class="max-w460"></el-input>
        </el-form-item>
      </div>
      <el-form-item label="打印聯數" prop="printTimes" :rules="[{required: true,message: ' '}]">
        <el-input v-model="form.printTimes" type="number" class="max-w460"></el-input>
        <div class="tips">同一訂單，打印的次數</div>
      </el-form-item>

      <el-form-item label="排序">
        <el-input v-model="form.sort" type="number" class="max-w460"></el-input>
        <div class="tips">數字越小越靠前</div>
      </el-form-item>

      <!-- 芯烨云打印机 -->
      <div v-if="form.printerType == '芯燁雲打印'">
        <el-form-item
            label="USER"
            prop="xpYun.USER"
            :rules="[{ required: true, message: ' ' }]"
        >
          <el-input v-model.trim="form.xpYun.USER" class="max-w460"></el-input>
          <div class="tips">芯燁雲開放平台開發者ID</div>
        </el-form-item>

        <el-form-item
            label="UKEY"
            prop="xpYun.UKEY"
            :rules="[{ required: true, message: ' ' }]"
        >
          <el-input v-model.trim="form.xpYun.UKEY" class="max-w460"></el-input>
          <div class="tips">芯燁雲開放平台生成的開發者密鑰</div>
        </el-form-item>

        <el-form-item
            label="打印機編號"
            prop="xpYun.SN"
            :rules="[{ required: true, message: ' ' }]"
        >
          <el-input v-model.trim="form.xpYun.SN" class="max-w460"></el-input>
          <div class="tips">查看芯燁雲打印機背面二維碼下的編號</div>
        </el-form-item>
      </div>

      <!--提交-->
      <div class="common-button-wrapper">
          <el-button type="primary" @click="onSubmit" :loading="loading">提交</el-button>
      </div>



    </el-form>


  </div>

</template>

<script>
import SettingApi from '@/api/setting.js';
export default {
  data() {
    return {
      /*切换菜单*/
      // activeIndex: '1',
      /*form表单数据*/
      form: {
        printerName: '',
        printerType: '',
        sort: 1,
        printTimes: '',
        feieYun: {
          USER: '',
          UKEY: '',
          SN: ''
        },
        printCenter: {
          deviceNo: '',
          key: ''
        },
        xpYun: {
          USER: "",
          UKEY: "",
          SN: ""
        }
      },
      loading: false,
      type: []
    };
  },
  created() {
    this.getData();
  },
  methods: {
    getData() {
      SettingApi.printerType({}, true).then(res => {
        this.type = res.data;
      }).catch(error => {});
    },
    //提交表单
    onSubmit() {
      let self = this;
      let form = self.form;
      self.$refs.form.validate(valid => {
        if (!form.printerType) {
          ElMessage.error("打印機類型不能為空");
          return;
        }
        if (valid) {
          self.loading = true;
          SettingApi.addPrinter(form, true).then(data => {
            self.loading = false;
            ElMessage({
              message: "恭喜您，新增成功",
              type: 'success'
            });
            self.$router.push('/setting/printer/index');
          }).catch(error => {
            self.loading = false;
          });
        }
      });
    }
  }
};
</script>

<style>
.tips {
    color: #ccc;
  }
</style>

